package com.kool.leitu.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kool.leitu.base.exception.AppException;
import com.kool.leitu.service.SqlService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @description:
 * @author: tianhe
 * @create: 2019/02/01 17:10
 */
@RestController
@Slf4j
public class SqlController {
    @Resource
    private SqlService sqlService;

    /**
     * excel转建表sql
     *
     * @param uploadFile
     * @return
     */
    @RequestMapping(value = "/dev/sql/gen",method = RequestMethod.POST)
    public JSONObject getSqlFromExcel(@RequestParam("file") MultipartFile uploadFile) {
        if (null == uploadFile) {
            return new JSONObject();
        }
        String sql = sqlService.excelToSql(uploadFile);

        JSONObject output = new JSONObject();
        JSONArray jaSec = new JSONArray();
        JSONObject joProp = new JSONObject();
        joProp.put("sql", sql);
        jaSec.add(joProp);
        output.put("a", jaSec);
        return output;
    }
}
